package com.nq.activaity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nq.activaity.entity.StSignInSetting;
import com.nq.activaity.entity.StUserInviteRecord;
import com.nq.activaity.service.IStSignInSettingService;
import com.nq.common.Result;
import com.nq.common.system.query.QueryGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 签到设置表
 * @Author: jeecg-boot
 * @Date:   2024-11-06
 * @Version: V1.0
 */
@RestController
@RequestMapping("/sign/stSignInSetting")
@Slf4j
public class StSignInSettingController {
	@Autowired
	private IStSignInSettingService stSignInSettingService;
	
	/**
	 * 分页列表查询
	 *
	 * @param stSignInSetting
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(StSignInSetting stSignInSetting,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<StSignInSetting> queryWrapper = QueryGenerator.initQueryWrapper(stSignInSetting, req.getParameterMap());
		Page<StSignInSetting> page = new Page<StSignInSetting>(pageNo, pageSize);
		IPage<StSignInSetting> pageList = stSignInSettingService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param stSignInSetting
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody StSignInSetting stSignInSetting) {

		Integer type = stSignInSetting.getSignInType();
		boolean isSame = true;
		List<StSignInSetting> list = stSignInSettingService.list();
		for (StSignInSetting record : list) {
			Integer signInType = record.getSignInType();
			if (!signInType.equals(type)) {
				isSame = false;
			}

		}

		if (!isSame) {
			return Result.error("只能设置同一类型的签到规则");
		}

		stSignInSettingService.save(stSignInSetting);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stSignInSetting
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody StSignInSetting stSignInSetting) {
		Integer type = stSignInSetting.getSignInType();
		boolean isSame = true;
		List<StSignInSetting> list = stSignInSettingService.list();
		for (StSignInSetting record : list) {
			Integer signInType = record.getSignInType();
			if (!signInType.equals(type)) {
				isSame = false;
			}

		}

		if (!isSame) {
			return Result.error("只能设置同一类型的签到规则");
		}

		stSignInSettingService.updateById(stSignInSetting);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		stSignInSettingService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stSignInSettingService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		StSignInSetting stSignInSetting = stSignInSettingService.getById(id);
		if(stSignInSetting==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stSignInSetting);
	}

//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param stSignInSetting
//    */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, StSignInSetting stSignInSetting) {
//        return super.exportXls(request, stSignInSetting, StSignInSetting.class, "签到设置表");
//    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, StSignInSetting.class);
//    }

}
